import 'package:car_map/mode/net_model/project_list_entity.dart';
import 'package:car_map/mode/time_scale.dart';
import 'package:car_map/net/request.dart';
import 'package:car_map/page/res/gaps.dart';
import 'package:car_map/page/routers/fluro_navigator.dart';
import 'package:car_map/widget/load_image.dart';
import 'package:car_map/widget/loading_widget.dart';
import 'package:car_map/widget/widget_grouping_title.dart';
import 'package:car_map/widget/widget_search_textfield.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';

import 'grouping_router.dart';
import 'groupingprovide/devicelist_provide.dart';

///分组--设备
class DevicePage extends StatefulWidget {
  @override
  _deviceState createState() => _deviceState();
}

class _deviceState extends State<DevicePage>
    with AutomaticKeepAliveClientMixin {
  List<TimeScale> scaleList;
  EasyRefreshController _controller;
  List<ProjectListEntity> projectList = new List();
  String DeviceStatus=null;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getTimeScaleList();
    _controller = new EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      padding: EdgeInsets.only(left: 10.0, right: 10.0),
      child: Provide<DeviceListProvide>(
        builder: (context, child, values) {
          print('设备id++++${values.deviceStatus}');
          if(DeviceStatus!=values.deviceStatus){
            DeviceStatus=values.deviceStatus;
            _controller.callRefresh();
          }

          return EasyRefresh(
            firstRefresh: true,
            controller: _controller,
            firstRefreshWidget: LoadingWidget(),
            onRefresh: () async {
              print('设备id-----${values.deviceStatus}');
              httpProjectList(values.deviceStatus);
            },
            child: SingleChildScrollView(
              child: Column(
                children: <Widget>[
                  WidgetGroupingTitle(
                    timeScale: scaleList,
                  ),
                  Gaps.vGap10,
                  WidgetSearchTextField(),
                  getWidgetDeviceList(),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  List<TimeScale> getTimeScaleList() {
    scaleList = new List();
    scaleList.add(new TimeScale(title: '全部(22)', index: 3));
    scaleList.add(new TimeScale(title: '在线(0)', index: 2));
    scaleList.add(new TimeScale(title: '离线(22)', index: 3));
    scaleList.add(new TimeScale(title: '过期(0)', index: 4));
  }

  //设备清单item
  Widget itemDevice(ProjectListEntity projectEntity) {
    return Container(
      decoration: BoxDecoration(
          border:
              Border(bottom: BorderSide(color: Colors.black12, width: 0.5))),
      child: ListTile(
        onTap: () {
          ///设备详情
          NavigatorUtils.push(context, GroupingRouter.deviceDetailsPage,
              replace: false);
        },
        title: Text(
          '${projectEntity.projName}',
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
        ),
        leading: LoadAssetImage(
          'grouping/ic_grading',
          width: ScreenUtil().setWidth(30.0),
          height: ScreenUtil().setHeight(30.0),
        ),
        trailing: Icon(Icons.navigate_next),
        subtitle: Text('(${projectEntity.projId}/${projectEntity.projType})'),
      ),
    );
  }

  Widget getWidgetDeviceList() {
    return ListView.builder(
      itemBuilder: (context, index) {
        return itemDevice(projectList[index]);
      },
      itemCount: projectList.length,
      shrinkWrap: true,
      physics: new NeverScrollableScrollPhysics(),
    );
  }

  void httpProjectList(String organId) async {
    await Request().getProjectListData(organId).then((data) {
      setState(() {
        projectList = data;
      });
    }).catchError((e) {});
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
